先將想用Line Bot 傳送回覆的圖片傳至雲端, 此篇範例所是將圖片傳至 iThome 的發文草稿中 (Markdown語法) 並複製該圖片的網址~
在前一篇**Day 10: Line Bot 互動式訊息回傳功能** 的 linebotFunc1.py 程式碼中, 修改以下內容:
在第 34 行, 增加以下內容 -
elif mtext == '@想看西藥&拿西藥':
try:
message = ImageSendMessage(
original_content_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg",
preview_image_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg"
)
line_bot_api.reply_message(event.reply_token, message)
except:
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text= 'Sorry~屁桃故障囉!'))
可以到 Line Bot 官方貼圖集 查詢你想要的貼圖, 找到你想要的貼圖後, 複製貼圖的 “Package ID” 和 “Sticker ID” ; 並在 linebotFunc1.py 第44 行增加以下程式碼:
elif mtext == '@想看中藥&拿中藥':
try:
message = StickerSendMessage(
package_id = '8522',
sticker_id = '16581289'
)
line_bot_api.reply_message(event.reply_token, message)
except:
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text= 'Sorry~屁桃故障囉!'))
結果如下圖:
截至目前為止, 示範的 linebotFunc1.py 程式碼如下:
from flask import Flask
app = Flask(__name__)
from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage,TextSendMessage, ImageSendMessage, StickerSendMessage, LocationSendMessage, QuickReply, QuickReplyButton, MessageAction
line_bot_api = LineBotApi('ZuqEf8yqUPeYP0Ekr9Q3tiv0fy5MVa7MUa9m/BuM9e1M3VBHRBEUjdSIz+Fesg+lzzqz05VbZTasXaWpDwcc3sJi10gft4S9cSMHDQ79jAum51T3qUPwrRzLIiugzovRQceNJ4ro67sVmtihY9lUUAdB04t89/1O/w1cDnyilFU=')
handler = WebhookHandler('3b121dfe3eadcd614bd6bd10cb18f0ca')
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
mtext = event.message.text
if mtext == '@確診後流程':
try:
message = TextSendMessage(
text = "確診後該怎麼辦?(SOP圖示)"
)
line_bot_api.reply_message(event.reply_token, message)
except:
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text= 'Sorry~屁桃故障囉!'))
elif mtext == '@想看西藥&拿西藥':
try:
message = ImageSendMessage(
original_content_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg",
preview_image_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg"
)
line_bot_api.reply_message(event.reply_token, message)
except:
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text= 'Sorry~屁桃故障囉!'))
elif mtext == '@想看中藥&拿中藥':
try:
message = StickerSendMessage(
package_id = '8522',
sticker_id = '16581289'
)
line_bot_api.reply_message(event.reply_token, message)
except:
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text= 'Sorry~屁桃故障囉!'))
if __name__ == '__main__':
app.run()
想更改 按了 “@確診後流程” 的圖文選單會出現 文字與圖片 (當然如果你想要傳送 ”貼圖“ 的話也可以在程式碼中的 message [ #串列中增加StickerSendMessage(....) ]
linebotFunc1.py 截至目前的code為:
from flask import Flask
app = Flask(__name__)
from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage,TextSendMessage, ImageSendMessage, StickerSendMessage, LocationSendMessage, QuickReply, QuickReplyButton, MessageAction
line_bot_api = LineBotApi('ZuqEf8yqUPeYP0Ekr9Q3tiv0fy5MVa7MUa9m/BuM9e1M3VBHRBEUjdSIz+Fesg+lzzqz05VbZTasXaWpDwcc3sJi10gft4S9cSMHDQ79jAum51T3qUPwrRzLIiugzovRQceNJ4ro67sVmtihY9lUUAdB04t89/1O/w1cDnyilFU=')
handler = WebhookHandler('3b121dfe3eadcd614bd6bd10cb18f0ca')
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
mtext = event.message.text
if mtext == '@確診後流程':
try:
message = [
TextSendMessage( #傳文字
text = "確診後該怎麼辦?(SOP圖示)"
),
ImageSendMessage( #傳圖片
original_content_url="https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg",
preview_image_url="https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg"
),
TextSendMessage( # 傳文字
text="小叮嚀: \n \n @因為看線上中醫的程序與需要等待的時間較久, 若想要中西藥都一起吃的話, 可以拿到西藥後 (比較快), 打電話請附近中藥行代煮**清冠一號***的水藥或磨成藥粉先吃, 再花時間處理看中醫需申請的證明, 程序等等...(若已有清冠一號後, 看中醫前, 可以參考圖文選單中間下面的**關於屁桃** > 看中/西醫的小訣竅) \n \n @中藥行的代煮清冠一號, 會請顧客到 **維基百科** 截圖中藥材的配方, 並傳至Line 上請他們代煮水藥或磨成藥粉 (水藥和藥粉的差異請點選 圖文選單中間下面的**關於屁桃** > 確診保養相關資訊)"
)]
line_bot_api.reply_message(event.reply_token, message)
except:
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text= 'Sorry~屁桃故障囉!'))
elif mtext == '@想看西藥&拿西藥':
try:
message = ImageSendMessage(
original_content_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg",
preview_image_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg"
)
line_bot_api.reply_message(event.reply_token, message)
except:
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text= 'Sorry~屁桃故障囉!'))
elif mtext == '@想看中藥&拿中藥':
try:
message = StickerSendMessage(
package_id = '8522',
sticker_id = '16581289'
)
line_bot_api.reply_message(event.reply_token, message)
except:
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text= 'Sorry~屁桃故障囉!'))
if __name__ == '__main__':
app.run()